/**
 * Created by Administrator on 2017/1/17.
 */

var MJ = require("./MJ.js");

function PublicCards(room) {
    this.room = room;

    this.cards = [];         // 牌
    this.getIndex = 0;      // 取牌位置
    this.getLastIndex = 0;  // 倒着取牌的位置
}

PublicCards.prototype = {

    // 生成牌组
    gen: function () {
        // 初始化数据
        var subConfig = this.room.getSubConfig();
        var colors = subConfig.colors;

        this.cards = [];
        this.getIndex = 0;

        // 生成牌
        var originCards = [];
        if (subConfig.setRangeZI) {
            colors.forEach(function (color) {
                var cardNum = MJ.ColorCardNum[color];

                if (color === MJ.Color.ZI) {
                    subConfig.setRangeZI.forEach(function (zi) {
                        var card = MJ.getValue(color, zi);
                        for (var i = 1, n = cardNum.num; i <= n; ++i) {
                            originCards.push(card)
                        }
                    })
                } else {
                    for (var p = 1, pm = cardNum.point; p <= pm; ++p) {
                        var card = MJ.getValue(color, p);
                        for (var i = 1, n = cardNum.num; i <= n; ++i) {
                            originCards.push(card);
                        }
                    }
                }
            });
        } else {
            colors.forEach(function (color) {
                var cardNum = MJ.ColorCardNum[color];
                for (var p = 1, pm = cardNum.point; p <= pm; ++p) {
                    var card = MJ.getValue(color, p);
                    for (var i = 1, n = cardNum.num; i <= n; ++i) {
                        originCards.push(card);
                    }
                }
            });
        }
        while (originCards.length > 0) {
            var iRamdom = Math.floor(Math.random() * originCards.length);
            this.cards.push(originCards[iRamdom]);
            originCards.splice(iRamdom, 1);
        }
        this.getLastIndex = this.cards.length - 1;
    },

    // 看下一张牌而不摸牌
    lookCard: function () {
        return (this.getIndex >= this.cards.length) ? 0 : this.cards[this.getIndex];
    },

    lookCards: function (num) {
        var cards = [];

        for (var i = 0, l = Math.min(num, this.cards.length - this.getIndex); i < l; ++i) {
            cards.push(this.cards[this.getIndex + i]);
        }

        return cards;
    },

    // 看牌堆中的最后一张牌
    lookLastCard: function () {
        return (this.getLastIndex >= this.getIndex) ? this.cards[this.getLastIndex] : 0;
    },

    // 倒着摸牌
    getLastCard: function (preCard) {
        if (preCard) {
            for (var i = this.getLastIndex; i >= this.getIndex; i--) {
                if (this.cards[i] == preCard) {
                    this.cards[i] = this.cards[this.getLastIndex];
                    this.cards[this.getLastIndex] = preCard;
                    break;
                }
            }
        }

        var card = this.cards[this.getLastIndex];
        this.getLastIndex -= 1;
        return card;
    },

    // 摸牌
    getCard: function (preCard) {
        if (preCard) {
            for (var ci = this.getIndex, cl = this.cards.length; ci < cl; ++ci) {
                if (this.cards[ci] == preCard) {
                    this.cards[ci] = this.cards[this.getIndex];
                    this.cards[this.getIndex] = preCard;
                    break;
                }
            }
        }

        var card = this.cards[this.getIndex];
        this.getIndex += 1;
        return card;
    },

    // 摸牌
    getCards: function (num, preCards) {
        var rtnCards = [];
        for (var i = 0; i < num; ++i) {
            if (preCards && i < preCards.length) {
                var preCard = preCards[i];
                for (var ci = this.getIndex, cl = this.cards.length; ci < cl; ++ci) {
                    if (this.cards[ci] == preCard) {
                        this.cards[ci] = this.cards[this.getIndex];
                        this.cards[this.getIndex] = preCard;
                        break;
                    }
                }
            }

            rtnCards.push(this.cards[this.getIndex]);
            ++this.getIndex;
        }
        return rtnCards;
    },

    // 公牌是否已经摸完
    isEmpty: function () {
        return this.getIndex >= this.cards.length;
    },

    // 获取剩余牌数量
    getRemainNum: function () {
        return this.cards.length - this.getIndex;
    },

    // 获取剩余公牌
    getRemainCards: function () {
        return this.cards.slice(this.getIndex);
    },

    setRemainCards: function (newCardsArray) {
        if (newCardsArray.length != this.getRemainNum()) {
            return;
        }

        var remainCards = MJ.buildFromArray(this.getRemainCards());
        var newCards = MJ.buildFromArray(newCardsArray);
        if (!MJ.isSameCards(remainCards, newCards)) {
            return;
        }

        for (var i = 0, l = newCardsArray.length; i < l; ++i) {
            this.cards[this.getIndex + i] = newCardsArray[i];
        }
    },
};

exports.PublicCards = PublicCards;